package gameUi
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	
	import gameUi.Event.LoadingEvent;

	public class LoadingUI extends Sprite
	{
		
		private var swfs:Array = new Array("../swc/bjResource.swf","../swc/card.swf","../swc/panel.swf","../swc/对话框资源.swf","../swc/警告对话框资源.swf");
		private var loader:Loader;
		private var loadNum:int;
		private var totleByte:Number=0;
		private var nowByte:Number=0;
		private var tempByte:Number = 0;
		
		public function LoadingUI()
		{
			loader = new Loader;
			mathLoad();
		}
		
		private function mathLoad(e:ProgressEvent=null):void{
			if(e) totleByte += e.bytesTotal;
			loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, mathLoad);
			if(loadNum<swfs.length){
				loader.load(new URLRequest(swfs[loadNum]));
				loadNum++;
				loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, mathLoad);
			}else{
				loadswf();
			}
		}
		
		private function loadswf():void {
			if(swfs.length > 0){
				loader.load(new URLRequest(swfs.pop()));
			}else{
				BlackJack.eventTower.dispatchEvent(new Event(LoadingEvent.LODING_SUCCESS));
			}
			addChild(loader);
			//loader.contentLoaderInfo.addEventListener(Event.INIT,init2);
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
		}//end of function loadswf
		private function loadProgress(event:ProgressEvent):void {
			tempByte = event.bytesTotal;
			var percentLoaded:Number = Math.max(event.bytesLoaded,nowByte+event.bytesLoaded)/totleByte;
			percentLoaded = Math.round(percentLoaded * 100);
			trace("Loading: "+percentLoaded+"%");
		}
		private function loadComplete(event:Event):void {
			trace("Complete");
			nowByte += tempByte;
			var swf_mc:*=loader.content;
			loadswf();
		}
	}
}